package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.AllocationOutMaterialsHaveSearchActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageDetailBatchActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageDetailSNActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/3/30 10:47
 * @email jianyu.zhan@inossem.com
 */
public class AllocationOutMaterialsHaveSearchAdapter extends BaseAdapter {

    private Context mContext;
    private List<ItemListBean> list;
    private String mCondition;
    private AllocationOutMaterialsHaveSearchActivity.OutSearchListener listener;
    private String location_id_output;

    private boolean isOk = false;
    private String mat_store_type_first;

    public AllocationOutMaterialsHaveSearchAdapter(Context mContext, List<ItemListBean> list, String mCondition, String location_id_output, AllocationOutMaterialsHaveSearchActivity.OutSearchListener listener) {
        this.mContext = mContext;
        this.list = list;
        this.mCondition = mCondition;
        this.location_id_output = location_id_output;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parentp) {
        if (position == 0) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_out_storage_picking_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setText(mCondition);
            condition.setHint("输入物料编码/物料描述");
            search.setOnClickListener(listener.getNoDoubleClickListener(condition));
        } else {
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.view_out_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            ItemListBean PickingOutstorageItem = list.get(position - 1);
            initData(mContext, viewHolder, convertView, PickingOutstorageItem);

            if (position==1){
                mat_store_type_first = PickingOutstorageItem.getMat_store_type();
            }
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView materielCode, materielDescription, outStorageNumber, rid, type;
        private PercentRelativeLayout headLayout;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.rid = (TextView) view.findViewById(R.id.rid);
        viewHolder.type = (TextView) view.findViewById(R.id.type);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ItemListBean pickingOutStorageItem) {
        viewHolder.rid.setText(pickingOutStorageItem.getRefer_receipt_rid());
        viewHolder.type.setText(pickingOutStorageItem.getMat_store_type());
        viewHolder.materielCode.setText(pickingOutStorageItem.getMat_code());
        viewHolder.materielDescription.setText(pickingOutStorageItem.getMat_name());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(pickingOutStorageItem.getActualSendNumber()) ? mContext.getResources().getString(R.string.activity_out_storage_sale_overview_real_hair)
                : (mContext.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + pickingOutStorageItem.getActualSendNumber() + pickingOutStorageItem.getUnit_name()));
        if (TextUtils.isEmpty(pickingOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
            isOk = true;
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Log.i("aaaa", viewHolder.type.getText().toString());
                switch (viewHolder.type.getText().toString()) {
                    case OutStorageUtils.TYPE_BATCH:
                        //批次
                        Intent intent = new Intent(context, OutStorageDetailBatchActivity.class);
                        intent.putExtra(OutStorageExtra.SERIAL, pickingOutStorageItem.getMat_id());
                        intent.putExtra(OutStorageExtra.TYPE, OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION);
                        intent.putExtra(AllocationOutStorageExtra.TYPE, 1);
                        intent.putExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_ID_OUTPUT, location_id_output);
                        context.startActivity(intent);
                        break;
                    case OutStorageUtils.TYPE_SN:
                        //序列号
                        intent = new Intent(context, OutStorageDetailSNActivity.class);
                        intent.putExtra(OutStorageExtra.SERIAL, pickingOutStorageItem.getMat_id());
                        intent.putExtra(OutStorageExtra.TYPE, OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION);
                        intent.putExtra(AllocationOutStorageExtra.TYPE, 1);
                        context.startActivity(intent);
                        break;
                    case OutStorageUtils.TYPE_NONE:
                        break;
                }
            }
        });
    }

    public boolean isOk() {
        return isOk;
    }

    public String getMat_store_type_first() {
        return mat_store_type_first;
    }
}
